package com.sls.nzdapplication.ui.fragment;

import android.content.ClipData;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.style.FontStyle;
import com.bin.david.form.data.table.TableData;
import com.esri.arcgisruntime.mapping.Item;
import com.sls.nzdapplication.R;
import com.sls.nzdapplication.db.utils.EquipmentUtil;
import com.sls.nzdapplication.db.utils.GyyhdUtil;
import com.sls.nzdapplication.model.Equipment;
import com.sls.nzdapplication.model.Gyyhd;
import com.sls.nzdapplication.model.ZyManagementEquipment;
import com.sls.nzdapplication.model.ZyWorkInfo;
import com.sls.nzdapplication.ui.activity.EquipmentActivity;
import com.sls.nzdapplication.ui.activity.GyyhdActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 这个类是一个Fragment，用于展示某一条线路的设备信息和工艺要求信息。
 * 在onViewCreated方法中，初始化了一个SmartTable，用于展示设备信息。
 * 在initData方法中，通过EquipmentUtil和GyyhdUtil查询了该线路下的设备信息和工艺要求信息，并将其展示在SmartTable中。
 * 在showData方法中，将查询到的设备信息和工艺要求信息封装成ZyManagementEquipment对象，并将其添加到SmartTable中。
 * 在SmartTable中，点击某一行设备信息，会跳转到EquipmentActivity或GyyhdActivity中，展示该设备的详细信息。
 */
public class ZyManagementEquipmentFragment extends Fragment {
    private EquipmentUtil equipmentUtil;

    private GyyhdUtil gyyhdUtil;

    private LinearLayout dataView;

    private List<Equipment> equipmentList;

    private String lineId;

    private SmartTable<ZyManagementEquipment> table;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.zy_equipment_manage_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lineId = getArguments().getString("lineId");
        table = view.findViewById(R.id.table);
        table.getConfig().setShowXSequence(false);
        table.getConfig().setShowYSequence(false);
        table.getConfig().setShowTableTitle(false);
        table.getConfig().setColumnTitleStyle(new FontStyle(30, getResources().getColor(R.color.colorPrimaryDark)));
        table.getConfig().setMinTableWidth(getActivity().getWindowManager().getDefaultDisplay().getWidth());
        initData();
    }

    private void initData() {
        equipmentUtil = new EquipmentUtil(getContext());
        gyyhdUtil = new GyyhdUtil(getContext());
        equipmentList = equipmentUtil.queryEquipmentByLineIdNOGt(lineId);
        showData(equipmentList);
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    /**
     * 数据
     */
    private void showData(List<Equipment> titleData) {
        table.setTableData(null); // 清空表格数据，准备设置新的设备数据
        List<ZyManagementEquipment> zyManagementEquipments = new ArrayList<>(); // 用于存储转换后的设备信息，以供表格显示
        int snumber = titleData.size(); // 存储标题数据的大小，用于循环遍历
        for (int i = 0; i < titleData.size(); i++) {
            final Equipment equipment = titleData.get(i); // 获取当前遍历到的设备信息
            ZyManagementEquipment zyManagementEquipment = new ZyManagementEquipment(); // 创建设备管理表格所需的行对象
            zyManagementEquipment.setXh(String.valueOf(i + 1)); // 设置设备序号
            zyManagementEquipment.setSbmc(equipment.getName()); // 设置设备名称
            // 尝试获取上一级设备的名称，如果不存在，则设置为空字符串
            Equipment equipment1 = equipmentUtil.listOneEquipment(equipment.getPreviousEquipmentId());
            if (null != equipment1) {
                zyManagementEquipment.setSjmc(equipment1.getName());
            } else {
                zyManagementEquipment.setSjmc("");
            }

            // 根据当前设备的运行状态，设置设备状态标记
            if (equipment.getSbzt().equals("停运") || equipment.getSbzt().equals("未投运")) {
                zyManagementEquipment.setZt("异常");
            } else {
                zyManagementEquipment.setZt("正常");
            }

            zyManagementEquipment.setSblx(equipment.getType()); // 设置设备类型
            zyManagementEquipments.add(zyManagementEquipment); // 将处理好的设备信息添加到列表中
        }

        List<Gyyhd> gyyhdList = gyyhdUtil.queryGyyhdByLineId(lineId);
        for (int i = 0; i < gyyhdList.size(); i++) {
            final Gyyhd equipment = gyyhdList.get(i);
            ZyManagementEquipment zyManagementEquipment = new ZyManagementEquipment();
            zyManagementEquipment.setXh(String.valueOf(i + 1 + snumber));
            zyManagementEquipment.setSbmc(equipment.getName());
            //设置状态正常
            zyManagementEquipment.setZt("正常");
            zyManagementEquipment.setSblx(equipment.getType());
            zyManagementEquipments.add(zyManagementEquipment);
        }

        if (zyManagementEquipments.size() > 0) {
            table.setVisibility(View.VISIBLE);
            table.setData(zyManagementEquipments);
            table.getTableData().setOnRowClickListener(new TableData.OnRowClickListener<ZyManagementEquipment>() {
                @Override
                public void onClick(Column column, ZyManagementEquipment zyManagementEquipment, int col, int row) {
                    if (!zyManagementEquipment.getSblx().equals("高压用户点")) {
                        Intent intent = new Intent(getActivity(), EquipmentActivity.class);
                        intent.putExtra("equipmentId", titleData.get(row).getId());
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), GyyhdActivity.class);
                        intent.putExtra("equipmentId", gyyhdList.get(row - snumber).getId());
                        startActivity(intent);
                    }

                }
            });
        } else {
            table.setVisibility(View.GONE);
        }

    }
}
